public class TesStringBuffer {
    public static void main(String[] args) {
        MyStringBuffer stringBuffer = new MyStringBuffer("aBcsaFS");
        System.out.println(stringBuffer);
        MyStringBuffer str = new MyStringBuffer("hehe");
        stringBuffer.inset(1,str);
        System.out.println(stringBuffer);
        stringBuffer.reverse();
        System.out.println(stringBuffer);
        str = stringBuffer.substring(3);
        System.out.println(str);
        str = stringBuffer.toUpperCase();
        System.out.println(str);
    }
}

class MyStringBuffer{
    private char[] string;
    private int count;
    public MyStringBuffer(){
        string = new char[16];
        count = string.length;
    }

    public MyStringBuffer(char[] chars){
        string = chars;
        count = string.length;
    }

    public MyStringBuffer(String s){
        char[] a = s.toCharArray();
        string = a;
        count = string.length;
    }

    public void expend(){
        char[] str = new char[string.length * 2];
        System.arraycopy(string, 0,str, 0, string.length);
        string = str;
    }

    public boolean Isfull() {
        if(string.length > count)
            return false;
        else
            return true;
    }

    public MyStringBuffer inset(int offset, MyStringBuffer s){
        if(offset < 0 || offset >= count)
            throw new ArrayIndexOutOfBoundsException();
        char[] str = s.string;
        if(Isfull())
            expend();

        System.arraycopy(string, offset, string, offset + str.length, count - offset);
        for(int i = 0;i < str.length; i++)
        {
            string[offset + i] = str[i];
        }
        count += str.length;
        MyStringBuffer buffer = new MyStringBuffer(string);
        return buffer;
    }

    public MyStringBuffer reverse(){
        int right = 0;
        int left = count - 1;
        while (right < left)
        {
            char tmp = string[right];
            string[right] = string[left];
            string[left] = tmp;
            right++;
            left--;
        }
        MyStringBuffer stringBuffer = new MyStringBuffer(string);
        return stringBuffer;
    }

    public MyStringBuffer substring(int begin){
        char[] chars = new char[count - begin];
        for(int i = 0; i < count - begin; i++)
            chars[i] = string[begin + i];
        MyStringBuffer stringBuffer = new MyStringBuffer(chars);
        return stringBuffer;
    }

    public MyStringBuffer toUpperCase(){
        char[] chars = string;
        for(int i = 0; i < count; i++)
        {
            if(chars[i] >= 'a' && chars[i] <= 'z')
                chars[i] -= 32;
        }
        MyStringBuffer stringBuffer = new MyStringBuffer(chars);
        return stringBuffer;
    }

    public String toString(){
        String str = new String(string);
        return str.substring(0,count);
    }
}
